package org.groupg.practice.tableInfo.model;

import org.apache.spark.sql.types.DataType;
import org.apache.spark.sql.types.DataTypes;

import java.util.Objects;
import java.util.StringJoiner;


public class ColumnInfo {
    private String columnCode;
    private String columnName;
    private String columnType;
    private String columnComment;

    public DataType getColumnSparkType() {
        return switch (columnType) {
            case "IntegerType" -> DataTypes.IntegerType;
            case "LongType" -> DataTypes.LongType;
            case "DoubleType" -> DataTypes.DoubleType;
            case "FloatType" -> DataTypes.FloatType;
            case "BooleanType" -> DataTypes.BooleanType;
            case "DateType" -> DataTypes.DateType;
            case "TimestampType" -> DataTypes.TimestampType;
            default -> DataTypes.StringType;
        };
    }

    public ColumnInfo() {
    }

    public String getColumnCode() {
        return columnCode;
    }

    public void setColumnCode(String columnCode) {
        this.columnCode = columnCode;
    }

    public String getColumnName() {
        return columnName;
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", ColumnInfo.class.getSimpleName() + "[", "]")
                .add("columnCode='" + columnCode + "'")
                .add("columnName='" + columnName + "'")
                .add("columnType=" + columnType)
                .add("columnComment='" + columnComment + "'")
                .toString();
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public String getColumnType() {
        return columnType;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ColumnInfo that = (ColumnInfo) o;
        return Objects.equals(getColumnCode(), that.getColumnCode()) && Objects.equals(getColumnName(), that.getColumnName()) && Objects.equals(getColumnType(), that.getColumnType()) && Objects.equals(getColumnComment(), that.getColumnComment());
    }

    @Override
    public int hashCode() {
        int result = Objects.hashCode(getColumnCode());
        result = 31 * result + Objects.hashCode(getColumnName());
        result = 31 * result + Objects.hashCode(getColumnType());
        result = 31 * result + Objects.hashCode(getColumnComment());
        return result;
    }

    public void setColumnType(String columnType) {
        this.columnType = columnType;
    }

    public String getColumnComment() {
        return columnComment;
    }

    public void setColumnComment(String columnComment) {
        this.columnComment = columnComment;
    }

    public ColumnInfo(String columnCode, String columnName, String columnType, String columnComment) {
        this.columnCode = columnCode;
        this.columnName = columnName;
        this.columnType = columnType;
        this.columnComment = columnComment;
    }
}
